<?php
//判断用户是否登录
if(!isset($_COOKIE['username']) || !isset($_COOKIE['qx'])){
	header('location:../login/login.php');
	exit;
}
//包含数据库配置文件
include '../../public/dbconfig.php';
//包含函数库文件
include '../../public/functions.php';
//调用函数 链接数据库
$link = con();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>主要内容区main</title>
<link href="../css/css.css" type="text/css" rel="stylesheet" />
<link href="../css/main.css" type="text/css" rel="stylesheet" />
<link rel="shortcut icon" href="../images/main/favicon.ico" />
<style>
body{overflow-x:hidden; background:#f2f0f5; padding:15px 0px 10px 5px;}
#searchmain{ font-size:12px;}
#search{ font-size:12px; background:#548fc9; margin:10px 10px 0 0; display:inline; width:100%; color:#FFF; float:left}
#search form span{height:40px; line-height:40px; padding:0 0px 0 10px; float:left;}
#search form input.text-word{height:24px; line-height:24px; width:180px; margin:8px 0 6px 0; padding:0 0px 0 10px; float:left; border:1px solid #FFF;}
#search form input.text-but{height:24px; line-height:24px; width:55px; background:url(../images/main/list_input.jpg) no-repeat left top; border:none; cursor:pointer; font-family:"Microsoft YaHei","Tahoma","Arial",'宋体'; color:#666; float:left; margin:8px 0 0 6px; display:inline;}
#search a.add{ background:url(../images/main/add.jpg) no-repeat -3px 7px #548fc9; padding:0 10px 0 26px; height:40px; line-height:40px; font-size:14px; font-weight:bold; color:#FFF; float:right}
#search a:hover.add{ text-decoration:underline; color:#d2e9ff;}
#main-tab{ border:1px solid #eaeaea; background:#FFF; font-size:12px;}
#main-tab th{ font-size:12px; background:url(../images/main/list_bg.jpg) repeat-x; height:32px; line-height:32px;}
#main-tab td{ font-size:12px; line-height:40px;}
#main-tab td a{ font-size:12px; color:#548fc9;}
#main-tab td a:hover{color:#565656; text-decoration:underline;}
.bordertop{ border-top:1px solid #ebebeb}
.borderright{ border-right:1px solid #ebebeb}
.borderbottom{ border-bottom:1px solid #ebebeb}
.borderleft{ border-left:1px solid #ebebeb}
.gray{ color:#dbdbdb;}
td.fenye{ padding:10px 0 0 0; text-align:right;}
.bggray{ background:#f9f9f9}
</style>
</head>
<body>
<!--main_top-->
<table width="99%" border="0" cellspacing="0" cellpadding="0" id="searchmain">
  <tr>
    <td width="99%" align="left" valign="top">您的位置：版块管理</td>
  </tr>
    <tr>
		<td align="left" valign="top">
			<table width="100%" border="0" cellspacing="0" cellpadding="0" id="search">
				<tr>
					<td width="90%" align="left" valign="middle">
					 <form method="get" action="list.php">
						 <span>版块名称：</span>
						 <input type="text" name="cname" value="" class="text-word">
						 <span>版块id：</span>
						 <input type="text" name="cid" value="" class="text-word">
						 <input name="" type="submit" value="搜索" class="text-but">
					 </form>
					</td>
					<td width="10%" align="center" valign="middle" style="text-align:right; width:150px;">
						<a href="add.php" target="mainFrame" onFocus="this.blur()" class="add">新增版块</a>
					</td>
				</tr>
			</table>
		</td>
    </tr>
	<tr>
		<td align="left" valign="top">
			<table width="100%" border="0" cellspacing="0" cellpadding="0" id="main-tab">
				<tr>
					<th align="center" valign="middle" class="borderright">编号id</th>
					<th align="center" valign="middle" class="borderright">所属分区</th>
					<th align="center" valign="middle" class="borderright">版块名称</th>
					<th align="center" valign="middle">操作</th>
				</tr>
				<?php
					//搜索版块名称
					if(!empty($_GET['cname'])){
						$cname = $_GET['cname'];
						$wheres[] = "cname like '%{$cname}%'";
						$urls[] = "cname={$cname}";
					}
					//搜索版块id
					if(!empty($_GET['cid'])){
						$cid = $_GET['cid'];
						$wheres[] = "cate.id like '%{$cid}%'";
						$urls[] = "id={$cid}";
					}
					//判断用户是否需要搜索
					if(is_array($wheres)){
						//拼接where子句
						$where = "where ".implode(' and ',$wheres);
						//拼接url
						$url = "&".implode('&',$urls);;
					}
					
					//当前页
					$page = isset($_GET['page']) ? $_GET['page'] : 1;
					//每页显示的条数
					$pagerows = 8;
					//当前页数 - 1 * 每页显示的条数
					$offset = ($page - 1) * $pagerows;
					//分页
					$limit = "limit {$offset},{$pagerows}";
					
					//准备sql
					$sql = "select cate.id,cate.pid,part.pname,cate.cname from cate left join part on part.id=cate.pid {$where} order by id {$limit}";
					//执行sql
					$res = mysqli_query($link, $sql);
					//判断结果集
					if($res && mysqli_num_rows($res)){
						while($cinfo = mysqli_fetch_assoc($res)){
				?>
				<tr onMouseOut="this.style.backgroundColor='#ffffff'" onMouseOver="this.style.backgroundColor='#edf5ff'">
					<td align="center" valign="middle" class="borderright borderbottom"><?php echo $cinfo['id'];?></td>
					<td align="center" valign="middle" class="borderright borderbottom"><?php echo $cinfo['pname'];?></td>
					<td align="center" valign="middle" class="borderright borderbottom"><?php echo $cinfo['cname'];?></td>
					<td align="center" valign="middle" class="borderbottom">
						<a href="edit.php?cid=<?php echo $cinfo['id']?>" target="mainFrame" onFocus="this.blur()" class="add">编辑</a><span class="gray">&nbsp;|&nbsp;</span>
						<a href="del.php?cid=<?php echo $cinfo['id'];?>" target="mainFrame" onFocus="this.blur()" class="add">删除</a>
					</td>
				</tr>
				<?php
						}
						//释放结果集
						mysqli_free_result($res);
					}
				?>
			</table>
		</td>
    </tr>
	<?php
		//查询共有多少条数据
		//准备sql
		$psql = "select count(*) from cate {$where}";
		//执行sql
		$pres = mysqli_query($link, $psql);
		//判断执行结果
		if($pres && mysqli_num_rows($pres)){
			$c = mysqli_fetch_row($pres);
		}
		//总页数
		$cpage = ceil($c[0] / $pagerows);
		//上一页
		$prevpage = $page - 1;
		if($prevpage < 1){
			$prevpage = 1;
		}
		//下一页
		$nextpage = $page + 1;
		if($nextpage > $cpage){
			$nextpage = $cpage;
		}
		//释放结果集
		mysqli_free_result($pres);
	?>
	<tr>
		<td align="left" valign="top" class="fenye"><?php echo $c[0];?> 条数据 <?php echo $page;?>/<?php echo $cpage;?> 页&nbsp;&nbsp;
		<a href="list.php?page=1<?php echo $url;?>" target="mainFrame" onFocus="this.blur()">首页</a>&nbsp;&nbsp;
		<a href="list.php?page=<?php echo $prevpage.$url;?>" target="mainFrame" onFocus="this.blur()">上一页</a>&nbsp;&nbsp;
		<a href="list.php?page=<?php echo $nextpage.$url;?>" target="mainFrame" onFocus="this.blur()">下一页</a>&nbsp;&nbsp;
		<a href="list.php?page=<?php echo $cpage.$url;?>" target="mainFrame" onFocus="this.blur()">尾页</a></td>
	</tr>
</table>
</body>
</html>
<?php
//关闭数据库链接
mysqli_close($link);
?>